package com.gxljc.bear.crawler.itaogao.util;

import com.gxljc.bear.crawler.base.DolphinCrawlerConf;
import com.gxljc.bear.crawler.itaogao.ItaogaoConst;
import com.gxljc.bear.crawler.itaogao.NewsPageTable;
import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;

import java.io.IOException;

/**
 * Created by Administrator on 2016/9/9.
 */
public class HbaseUtil {

    //保存html至hbase
    public static boolean saveHbase(NewsPageTable table, byte[] htmlByte) {
        try {
            Configuration conf = DolphinCrawlerConf.getInstance();
            HTable htable = new HTable(conf, ItaogaoConst.HBASE_TABLE_PAGE);
            String rowKey = table.id;
            Put put = new Put(org.apache.hadoop.hbase.util.Bytes.toBytes(rowKey));
            if (htmlByte != null) {
                put.add(org.apache.hadoop.hbase.util.Bytes.toBytes(ItaogaoConst.HBASE_FAMILY_DATA),
                        org.apache.hadoop.hbase.util.Bytes.toBytes(ItaogaoConst.SCHEMA_COLUMN_NAME.HTML.getValue()), htmlByte);
            }
            if (StringUtils.isNotEmpty(table.title)) {
                put.add(org.apache.hadoop.hbase.util.Bytes.toBytes(ItaogaoConst.HBASE_FAMILY_DATA),
                        org.apache.hadoop.hbase.util.Bytes.toBytes(ItaogaoConst.SCHEMA_COLUMN_NAME.TITLE.getValue()),
                        org.apache.hadoop.hbase.util.Bytes.toBytes(table.title));
            }
            try {
                htable.put(put);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }

        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }
}
